package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.mvp.model.IShippingAddressActivityModel;
import com.wgts.wgts.mvp.presenter.IShippingAddressActivityPresenter;
import com.wgts.wgts.mvp.view.IShippingAddressActivityView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.resp.CurrentEntity;
import com.wgts.wgts.netmodle.resp.ShippingAddressEntity;

import java.util.ArrayList;
import java.util.List;

/*
 *
 *
 *   data:2018/10/49:17
 *
 */public class ShippingAddressActivityPresenter implements IShippingAddressActivityPresenter {
     private IShippingAddressActivityView view;
     private IShippingAddressActivityModel model;
     private List<ShippingAddressEntity> list = new ArrayList<>();

    public ShippingAddressActivityPresenter(IShippingAddressActivityView view, IShippingAddressActivityModel model) {
        this.view = view;
        this.model = model;
    }

    @Override
    public void getShippingAddress() {
        model.getShippingAddress(new HttpResultSubscriber<List<ShippingAddressEntity>>() {
            @Override
            public void onSuccess(List<ShippingAddressEntity> entityList) {
                list.clear();
                list.addAll(entityList);
                view.getShippingAddress(list);
            }

            @Override
            public void _onError(int status, String msg) {

            }
        });
    }

    @Override
    public void normalAddressResult(String addressId) {
        model.normalAddressResult(addressId, new HttpResultSubscriber<CurrentEntity>() {
            @Override
            public void onSuccess(CurrentEntity currentEntity) {
                view.dateNotify();
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void deleteAddress(String addressId) {
        model.deleteAddress(addressId, new HttpResultSubscriber<CurrentEntity>() {
            @Override
            public void onSuccess(CurrentEntity currentEntity) {
                view.dateNotify();
            }

            @Override
            public void _onError(int status, String msg) {

            }
        });
    }

    @Override
    public void affirmAddressResult(int position) {
        view.affirmAddressResult(list.get(position));
    }

    @Override
    public void getSelectAddress() {

        if(list.isEmpty()){
            view.affirmAddressResult(null);
        }


        for (int i = 0; i < list.size(); i++){
            if (list.get(i).addressState.equals("1")){
                view.affirmAddressResult(list.get(i));
            }
        }
    }
}
